package com.bestplus.daqingapp.common.step;

import android.util.Log;

import com.bestplus.daqingapp.BuildConfig;
import com.bestplus.daqingapp.common.util.DataUtil;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.pgyersdk.crash.PgyCrashManager;

public class PedometerModule extends ReactContextBaseJavaModule {


    public PedometerModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "XPJPedometerUtil";
    }

    @ReactMethod
    public void readHealthyKitData(String type, int day, Callback callback) {
        if ("step".equals(type)) {
            callback.invoke(getSteps(day));
        } else if ("distance".equals(type)) {
            callback.invoke(getSteps(day) * 0.65F);
        }
    }

    @ReactMethod
    public void readHealthyKitDataAndSyn(int step, String day, Callback callback) {
        this.getSteps(step,day,callback);
    }

    @ReactMethod
    public void readSteps(Callback callback) {
        callback.invoke(getSteps());
    }

    @ReactMethod
    public void readInstance(Callback callback) {
        callback.invoke(getSteps() * 0.65F);
    }


    private void getSteps(int serverStep,String serverDay,Callback callback) {
        int  steps = (int) Pedometer.getSelf().getStepDetector(serverStep,serverDay);
        if(steps == -1){
            PgyCrashManager.reportCaughtException(getReactApplicationContext(), 
                new Exception("serverDay:"+serverDay+","+Pedometer.getSelf().getTime()));
        }
        callback.invoke(steps);
     

    }

    private int getSteps() {
        float steps;
        if (Pedometer.getSelf() == null) {
            steps = -100;
            // steps = DataUtil.readFloat(getReactApplicationContext(), StepCount.STEP_TODAY);
        } else {
            steps = Pedometer.getSelf().getStepDetector();
        }
        if (BuildConfig.DEBUG)
            Log.i("log", "readSteps: " + steps);
        return (int) steps;
    }

    private int getSteps(int day) {
        float steps;
        if (Pedometer.getSelf() == null) {
            steps = -100;
            // steps = DataUtil.readFloat(getReactApplicationContext(), StepCount.STEP_TODAY);
        } else {
            steps = Pedometer.getSelf().getStepDetector(day);
        }
        if (BuildConfig.DEBUG)
            Log.i("log", "readSteps: " + steps);
        return (int) steps;
    }


}
